package wangwenjun.phase2.concurrencypattern.threadcontext.context2;

import lombok.Data;

/**
 * @author ChangLiang
 * @date 2020/6/15
 */
@Data
public class ThreadContextV1 {

    /**
     * 在这个Context 例子中，我们将所有需要被线程
     * 访问和操作的数据都封装在了Context 中，每一个线程拥有不一样的上下文数据
     *
     * 只用一个ThreadLocal ，这也就意味着与之对应的ThreadLocalMap 有一份
     * Entry ，其中Key 是ThreadLocal, Value 是Context
     */
    private static final ThreadLocal<Context> context = ThreadLocal.withInitial(Context::new);

    public static Context get() {
        return context.get();
    }

    public static class Context{
        private Configuration configuration;

        private OtherResource otherResource;

        public Configuration getConfiguration() {
            return configuration;
        }

        public void setConfiguration(Configuration configuration) {
            this.configuration = configuration;
        }

        public OtherResource getOtherResource() {
            return otherResource;
        }

        public void setOtherResource(OtherResource otherResource) {
            this.otherResource = otherResource;
        }
    }
}
